package _10_binary_tree.exerc;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author: mornd
 * @dateTime: 2023/6/12 - 14:39
 * 根据后缀表达式构建数
 * 要求：表达式是正确的
 *
 * 21-3*
 *
 *          *
 *         /\
 *        -  3
 *       /\
 *      2  1
 */
public class ConstructExpressionTreeTest {

    @Test
    public void test() {
        String arr[] = {"2", "1", "-", "3", "*"};

        Deque<TreeNode> stack = new LinkedList<>();

        for (int i = 0; i < arr.length; i++) {
            String s = arr[i];
            switch (s) {
                case "+", "-", "*", "/" -> {
                    TreeNode right = stack.pop();
                    TreeNode left = stack.pop();
                    stack.push(new TreeNode(s, left, right));
                }
                default -> {
                    stack.push(new TreeNode(s));
                }
            }
        }

        System.out.println(stack.peek());
    }


    static class TreeNode {
        String val;
        TreeNode left;
        TreeNode right;

        public TreeNode(String val) {
            this.val = val;
        }

        public TreeNode(String val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
